# -*- coding: utf-8 -*-
import smtplib
# import sys
# reload(sys)
# sys.setdefaultencoding('utf8')
# from Program.settings import MAIL_HOST, MAIL_PASS, MAIL_USER, MAIL_POSTFIX
# from utils.logger import Md_Debug
import smtplib
from email.mime.text import MIMEText
from email.header import Header

# from utils.tel_num import TEL_NUM
TEL_NUM = "17791269154"
# MAIL_HOST = "smtp.163.com"
# MAIL_USER = "gooliangcloud@163.com"
# MAIL_PASS = "guliang1717"
# MAIL_POSTFIX = "163.com"
# MAIL_HOST = "smtp.163.com"
# MAIL_USER = "17629202572@163.com"
# MAIL_PASS = "960722zx"
# MAIL_PASS = "wangqian123"
# MAIL_POSTFIX = "163.com"

MAIL_HOST = "smtp.163.com"
MAIL_USER = "dintonhr@163.com"
MAIL_PASS = "dinton123"
MAIL_POSTFIX = "163.com"


def send_mail(mailto, subject, body, format='plain'):
    # 下面一行要设置成你自己的邮件服务器的地址以及用户名密码发件人信息
    # host, user, password, fromMail = "242174049@qq.com"

    if isinstance(body, unicode):
        body = str(body)
    me = ("%s<" + MAIL_USER + "@" + MAIL_POSTFIX + ">") % (Header("鼎拓科技", 'utf-8'),)
    msg = MIMEText(body, format, 'utf-8')
    if not isinstance(subject, unicode):
        subject = unicode(subject)
    msg['Subject'] = subject
    msg['From'] = me
    msg['To'] = mailto
    msg["Accept-Language"] = "zh-CN"
    msg["Accept-Charset"] = "ISO-8859-1,utf-8"
    try:
        s = smtplib.SMTP_SSL(MAIL_HOST, 465)
        s.connect(MAIL_HOST)
        s.login(MAIL_USER, MAIL_PASS)
        s.sendmail(me, mailto, msg.as_string())
        s.close()
        return True
    except Exception, e:
        print str(e)
        return False


def email_public(sign, content_param):
    """邮件发送接口"""
    if sign == 0:
        if not content_param['user_email']:
            print("user_email is null..")
            return False
        else:
            content = "{user_name}您好，西安鼎拓信息科技有限公司诚邀您{date}" \
                      "携带个人简历来公司参加面试。地址：西安高新区丈八一路绿地soho同盟b座710" \
                      "室，有问题可拨{tel_num}联系。收到请回复，谢谢！"

            content_info = content.format(user_name=content_param['user_name'], date=content_param['date'],
                                          tel_num=TEL_NUM)
            if send_mail(content_param['user_email'], "interview", content_info, format='plain'):
                return True
            else:
                return False


if __name__ == '__main__':
    mailto_list = 'zhangxin183913@163.com'  # 收件人(列表)
    mail_subject = "wenhou"
    mail_content = "你好wangqian"  # 邮件内容
    sign = 0
    content_param = {'date': '2018-10-8 16:58', 'user_name': u'zhanghxin',
     'user_email': u'zhangxin183913@163.com'}

    email_public(sign, content_param)
    # if send_mail(mailto_list, mail_subject, mail_content, format='plain'):
    #     print "done!"
    # else:
    #     print "failed!"
        # content_param = {"user_email":"zhangxin183913@163.com", "user_name":"张欣", "date":"2018年9月26日15:30"}
        # a = email_public(0,content_param)
        # print a



